package com.second.taobao.controller;

import com.google.gson.Gson;
import com.second.taobao.topic.GetTopic;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;


@Controller
@RequestMapping("/user")
public class UserController {

    @RequestMapping("/topic")
    @ResponseBody
    public void getUser(HttpServletRequest request, HttpServletResponse response) {
        String uid = request.getParameter("userid");

        Map map = new HashMap();
        PrintWriter out = null;
        Gson gson =new Gson();
        String json = "";

        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");

        try {
            out = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }

        //参数判空处理
        if(StringUtils.isEmpty(uid)){
            map.put("code", "401");
            map.put("data", "参数为空");

            out.write(gson.toJson(map));
            out.flush();
        }
          
        try {
            GetTopic topic = new GetTopic();
            map.put("code", "200");
            map.put("data", topic.get("topic_user", uid));
        } catch (Exception e) {
            map.put("code", "402");
            map.put("data", "处理异常");
        }

        out.write(gson.toJson(map));
        out.flush();
    }
}
